package com.wwwarehouse.usercenter.fragment.permission_recycle;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.permission_recycle.RetrieveAuthorityBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.eventbus_event.PREvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by xiaoke.ni on 2017/10/11.
 */

public class PRSuccessFragment extends BaseFragment implements View.OnClickListener {

    private View mView;

    private StateLayout mStateLayout;
    private ImageView mPrSuccessIv;
    private TextView mPrAdministratorSuccessTv;
    private TextView mLine1Tv;
    private TextView mLine2Tv;
    private Button mPrSeeNotSuccessBtn;
    private LinearLayout mPrSuccessBtnLl;
    private Button mNextBtn;
    private Button mFinishBtn;
    private Button mPrErrorBtn;

    private List<String> beUseIds;
    private String businessId;
    private String cardId;
    private RetrieveAuthorityBean retrieveAuthorityBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_pr_success, null);
        return mView;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.pr_next_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            popFragment();
            EventBus.getDefault().post(new PREvent("pr_refresh"));
        } else if (id == R.id.pr_finish_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        } else if (id == R.id.pr_error_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            popFragment();
            EventBus.getDefault().post(new PREvent("pr_refresh"));
        } else if (id == R.id.pr_see_not_success_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            PRUncollectedDetailFragment prUncollectedDetailFragment = new PRUncollectedDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("details", (ArrayList<? extends Parcelable>) retrieveAuthorityBean.getDetails());
            prUncollectedDetailFragment.setArguments(bundle);
            pushFragment(prUncollectedDetailFragment);
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PRSuccessFragment) {
            mActivity.setTitle(getString(R.string.user_pr));
        }
    }

    @Override
    public void requestDatas() {
        initView();
        Bundle bundle = getArguments();
        if (bundle != null) {
            beUseIds = bundle.getStringArrayList("beUseIds");
            businessId = bundle.getString("businessId");
            cardId = bundle.getString("cardId");
        }
        ((BaseCardDeskActivity) mActivity).hideBackBt();
        retrieveAuthority();
    }

    private void initView() {
        mStateLayout = (StateLayout) mView.findViewById(R.id.slt);
        mPrSuccessIv = (ImageView) mView.findViewById(R.id.pr_success_iv);
        mPrAdministratorSuccessTv = (TextView) mView.findViewById(R.id.pr_administrator_success_tv);
        mLine1Tv = (TextView) mView.findViewById(R.id.line1_tv);
        mLine2Tv = (TextView) mView.findViewById(R.id.line2_tv);
        mPrSeeNotSuccessBtn = (Button) mView.findViewById(R.id.pr_see_not_success_btn);
        mPrSuccessBtnLl = (LinearLayout) mView.findViewById(R.id.pr_success_btn_ll);
        mNextBtn = (Button) mView.findViewById(R.id.pr_next_btn);
        mFinishBtn = (Button) mView.findViewById(R.id.pr_finish_btn);
        mPrErrorBtn = (Button) mView.findViewById(R.id.pr_error_btn);
        mPrSeeNotSuccessBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
        mFinishBtn.setOnClickListener(this);
        mPrErrorBtn.setOnClickListener(this);
    }

    /**
     * 权限回收-回收权限
     */
    private void retrieveAuthority() {
        mStateLayout.showProgressView(true);
        Map<String, Object> map = new HashMap<>();
        map.put("beUseIds", beUseIds);
        map.put("businessId", businessId);
        map.put("cardId", cardId);
        NoHttpUtils.httpPost(UserCenterConstant.RETRIEVE_AUTHORITY, map, onResponseListener, 0);
    }


    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        mStateLayout.showContentView();
                        retrieveAuthorityBean = JSON.parseObject(commonClass.getData().toString(), RetrieveAuthorityBean.class);
                        int status = Integer.parseInt(retrieveAuthorityBean.getStatus());
                        //回收结果.0-成功 1-失败 2-部分失败 3-异常
                        switch (status) {
                            case 0:
                                mPrSuccessIv.setImageResource(R.drawable.icon_success);
                                mPrAdministratorSuccessTv.setText(getString(R.string.user_pr_success));
                                mLine1Tv.setText(getString(R.string.user_pr_name_authority) + retrieveAuthorityBean.getNameDesc());
                                if (retrieveAuthorityBean.getUsers() != null) {
                                    if (retrieveAuthorityBean.getUsers().size() > 1)
                                        mLine2Tv.setText(getString(R.string.user_pr_recycling_people) + retrieveAuthorityBean.getUsers().get(0) + getString(R.string.user_pr_etc) + retrieveAuthorityBean.getUsers().size() + getString(R.string.user_pr_people));
                                    else
                                        mLine2Tv.setText(getString(R.string.user_pr_recycling_people) + retrieveAuthorityBean.getUsers().get(0));
                                }
                                mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                mPrErrorBtn.setVisibility(View.GONE);
                                mPrSeeNotSuccessBtn.setVisibility(View.GONE);
                                break;
                            case 1:
                                mPrSuccessIv.setImageResource(R.drawable.icon_failure);
                                mPrAdministratorSuccessTv.setText(getString(R.string.user_pr_recycling_fail));
                                mLine1Tv.setText(getString(R.string.user_pr_name_authority)+ retrieveAuthorityBean.getNameDesc());
                                mLine2Tv.setText("");
                                mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                mPrErrorBtn.setVisibility(View.GONE);
                                mPrSeeNotSuccessBtn.setVisibility(View.VISIBLE);
                                break;
                            case 2:
                                mPrSuccessIv.setImageResource(R.drawable.icon_success);
                                mPrAdministratorSuccessTv.setText(getString(R.string.user_pr_part_recycling_success));
                                mLine1Tv.setText(getString(R.string.user_pr_name_authority) + retrieveAuthorityBean.getNameDesc());
                                if (retrieveAuthorityBean.getUsers() != null) {
                                    if (retrieveAuthorityBean.getUsers().size() > 1)
                                        mLine2Tv.setText(getString(R.string.user_pr_recycling_people) + retrieveAuthorityBean.getUsers().get(0) + getString(R.string.user_pr_etc) + retrieveAuthorityBean.getUsers().size() + getString(R.string.user_pr_people));
                                    else
                                        mLine2Tv.setText(getString(R.string.user_pr_recycling_people) + retrieveAuthorityBean.getUsers().get(0));
                                }
                                mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                mPrErrorBtn.setVisibility(View.GONE);
                                mPrSeeNotSuccessBtn.setVisibility(View.VISIBLE);
                                break;
                            case 3:
                                mPrSuccessIv.setImageResource(R.drawable.icon_failure);
                                mPrAdministratorSuccessTv.setText(getString(R.string.permission_notfound_txt));
                                mLine1Tv.setText(retrieveAuthorityBean.getNameDesc() + getString(R.string.permission_description_txt_1));
                                mLine2Tv.setText(getString(R.string.permission_description_txt_2));
                                mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                mPrErrorBtn.setVisibility(View.GONE);
                                mPrSeeNotSuccessBtn.setVisibility(View.GONE);
                                break;
                        }
                    } else {
                        mStateLayout.showEmptyView(commonClass.getMsg(), true);
                        mStateLayout.setEmptyListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                retrieveAuthority();
                            }
                        });
                        ToastUtils.showToast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    retrieveAuthority();
                }
            });
            dismissProgressDialog();

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };
}
